package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.dto.BoxCreateDto;
import com.example.front.dto.BoxDto;
import com.example.front.dto.BoxSearchDto;
import com.example.front.entity.Box;
import com.example.front.entity.BoxEventUser;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface BoxMapper {

    @Insert("INSERT INTO `box`" +
            " (`tenant_id`,`equ_id`,`gateway_id`,`line_id`,`category_id`,`name`,`dev_addr`,`craft_id`) VALUES" +
            " (#{tenantId},#{equId}, #{gatewayId}, #{lineId}, #{categoryId}, #{name}, #{devAddr},#{craftId})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Long create(BoxCreateDto box);

//    @Insert("INSERT INTO `box_event_user`" +
//            " (`box_id`,`event_id`,`user_id`,`level_id`) VALUES" +
//            " (#{boxId}, #{eventId}, #{userId},#{levelId})")
//    void boxEventUsercreate(BoxEventUser box);

//    @Select("<script>" +
//            "SELECT * FROM `box_event_user`" +
//            "<where>" +
//            "<if test='id!=null'> AND `id`=#{id}</if>" +
//            "<if test='boxId!=null'> AND `box_id`=#{boxId} </if>" +
//            "<if test='eventId!=null'> AND `event_id`=#{eventId} </if>" +
//            "AND `status`!=" + User.STATUS_DELETE +
//            "</where>" +
//            "ORDER BY event_id"+
//            "</script>")
//    List<BoxEventUser> boxEventUserList(BoxEventUser condition);

    @Update("<script>" +
            "UPDATE `box` " +
            "<set>" +
                "<if test='box.name!=null'>`name`= #{box.name},</if>" +
                "<if test='box.gatewayId!=null'>`gateway_id`= #{box.gatewayId}, </if>" +
                "<if test='box.lineId!=null'>`line_id`= #{box.lineId},</if>" +
                "<if test='box.equId!=null'>`equ_id`= #{box.equId},</if>" +
                "<if test='box.categoryId!=null'>`category_id`= #{box.categoryId},</if>" +
                "<if test='box.devAddr!=null'> `dev_addr`= #{box.devAddr},</if>" +
                "<if test='box.craftId!=null'>  `craft_id`= #{box.craftId},</if>" +
            "</set>" +
            " WHERE `id`=#{box.id} AND `tenant_id`=#{box.tenantId}  AND `status`!=" + User.STATUS_DELETE+
            "</script>")
    int update(@Param("box") Box box);

    @Select("<script>" +
            "SELECT * FROM `box`" +
            "<where>" +
            "<if test='condition.id!=null'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.devAddr!=null'> AND `dev_addr`=#{condition.devAddr}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%') </if>" +
            "<if test='condition.gatewayId!=null'> AND `gateway_id`=#{condition.gatewayId} </if>" +
            "<if test='condition.lineId!=null'> AND `line_id`=#{condition.lineId} </if>" +
            "<if test='condition.categoryId!=null'> AND `category_id` =#{condition.categoryId} </if>" +
            "<if test='condition.craftId!=null'> AND `craft_id` =#{condition.craftId} </if>" +
            " AND `tenant_id`=#{condition.tenantId} " +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            " ORDER BY `id` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<Box> findAll(@Param("condition") BoxSearchDto condition, @Param("pagination") Pagination pagination);

    @Select("<script>" +
            "SELECT count(*) FROM `box`" +
            "<where>" +
            "<if test='condition.id!=null'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.devAddr!=null'> AND `dev_addr`=#{condition.devAddr}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%') </if>" +
            "<if test='condition.gatewayId!=null'> AND `gateway_id`=#{condition.gatewayId} </if>" +
            "<if test='condition.lineId!=null'> AND `line_id`=#{condition.lineId} </if>" +
            "<if test='condition.categoryId!=null'> AND `category_id` =#{condition.categoryId} </if>" +
            "<if test='condition.craftId!=null'> AND `craft_id` =#{condition.craftId} </if>" +
            " AND `tenant_id`=#{condition.tenantId} " +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    long count(@Param("condition") BoxSearchDto condition);

    @Update("UPDATE `box` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{id} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int deleteById(long id, long tenantId);

//    @Update("UPDATE `box_event_user` SET `status` ="+ User.STATUS_DELETE+
//            " WHERE `user_id`=#{userId} AND `status`!=" + User.STATUS_DELETE)
//    int deleteBoxEventUserbyUserId(long userId);


    @Delete("delete from box_event_user where box_id=#{id}")
    int deleteBoxEventUser(long id);

    @Select("<script>" +
            "SELECT * FROM box WHERE `tenant_id` = #{tenantId} AND `name` = #{boxDto.name}" +
            "<if test='boxDto.id!=null'> AND `id`!=#{boxDto.id} </if> AND `status`!=" + User.STATUS_DELETE +
            " LIMIT 1" +
            "</script>")
    Box findByName(@Param("tenantId") long tenantId, @Param("boxDto") BoxDto boxDto);

    @Select("SELECT * FROM box WHERE `tenant_id` = #{tenantId} AND `Gateway_id` = #{GatewayId} AND `status`!=" + User.STATUS_DELETE)
    List<Box> findByGateway(@Param("tenantId") long tenantId, @Param("GatewayId") long GatewayId);

    @Select("SELECT  *  FROM box WHERE `tenant_id` = #{tenantId} AND `gateway_id` = #{gatewayId}  AND `dev_addr` = #{devAddr} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Box findByGatewayAddr(BoxDto box);

    @Select("SELECT * FROM box WHERE `tenant_id` = #{tenantId} AND `id` = #{id} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Box findById(@Param("tenantId") long tenantId, @Param("id") long id );

    @Select("SELECT * FROM box WHERE`id` = #{id} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Box findByID(@Param("id") long id );

    @Select("SELECT count(*) FROM box WHERE `tenant_id` = #{tenantId} AND `line_id` = #{lineId} AND `status`!=" + User.STATUS_DELETE)
    long findByLineId(@Param("tenantId") long tenantId, @Param("lineId") long lineId );

    @Select("SELECT count(*) FROM box WHERE `tenant_id` = #{tenantId} AND `gateway_id` = #{gatewayId} AND `status`!=" + User.STATUS_DELETE)
    long findByGatewayId(@Param("tenantId") long tenantId, @Param("gatewayId") long gatewayId );

    @Select("SELECT count(*) FROM box WHERE `tenant_id` = #{tenantId} AND `category_id` = #{categoryId} AND `status`!=" + User.STATUS_DELETE)
    long findByCategoryId(@Param("tenantId") long tenantId, @Param("categoryId") long categoryId );

    @Select("SELECT * FROM box WHERE `craft_id` = #{craftId} AND `status`!=" + User.STATUS_DELETE)
    Box findByCraftId(@Param("craftId") long craftId);
}
